﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using StaticPlugin;

namespace MosWar
{
    public class SkillsDetector
    {

        public static HtmlElement getPerson(WebBrowser b) 
        {
            if (b.Document == null || b.Document.Body == null) return null;
            return b.Document.Body.GetElementsByTagName("div")["personal"];
        }

        public static int getOil(WebBrowser b)
        {
            HtmlElement Person = getPerson(b);
            if (Person == null) return -1;
            //HtmlElement wallet = WebParserEx.getTagByClassName(Person, "ul", "wallet");
            string oil = WebParserEx.getTagByClassName(Person, "li", "neft-block").GetAttribute("title");
            oil = oil.Split(' ')[1];
            try
            {
                return int.Parse(oil);
            }
            catch (Exception)
            {
                Console.WriteLine("SkillsDetector::getMoney() - cannot parse tugriki=" + oil);
                return -1;
            }
        }

        public static int getRuda(WebBrowser b)
        {
            HtmlElement Person = getPerson(b);
            if (Person == null) return -1;
            //HtmlElement wallet = WebParserEx.getTagByClassName(Person, "ul", "wallet");
            string ruda = WebParserEx.getTagByClassName(Person, "li", "ruda-block").GetAttribute("title");
            if (ruda.Split(' ').Count() > 1) ruda = ruda.Split(' ')[1]; else ruda = ruda.Replace(",", "");
            try
            {
                return int.Parse(ruda);
            }
            catch (Exception)
            {
                Console.WriteLine("SkillsDetector::getMoney() - cannot parse tugriki=" + ruda);
                return -1;
            }
        }

        public static HtmlElement elemMainTimeout(WebBrowser b) 
        {
            HtmlElement HeaderLinks = WebParserEx.getTagByClassName(b.Document.Body, "div", "header clear");
            if (HeaderLinks == null)
            {
                Console.WriteLine("jGrabZakoulki::testRequirements=false");
                return null;
            }
            HtmlElement timeout = HeaderLinks.GetElementsByTagName("a")["timeout"];
            return timeout;
        }

        public static DateTime MainTimeoutElapsDate(WebBrowser b) 
        {
            DateTime timeToActivate=DateTime.Now;
            string Url = "";
            GetMainTmeoutStatus(b, out timeToActivate, out Url);
            return timeToActivate;
        }

        public static void GetMainTmeoutStatus(WebBrowser b, out DateTime timeToActivate, out string Url) 
        {
            timeToActivate = DateTime.MinValue; // давно-давно таймаут истекла
            Url = null;
            HtmlElement timeouts = elemMainTimeout(b);
            if (timeouts == null) return;
            if (timeouts.Style != null && timeouts.Style.IndexOf("DISPLAY: none") > -1) return;
            double d_ts = double.Parse(timeouts.GetAttribute("timer"));
            TimeSpan ts = TimeSpan.FromSeconds(d_ts);
            timeToActivate = DateTime.Now;
            timeToActivate = timeToActivate.Add(ts);
            Url = timeouts.GetAttribute("href");
        }

        public static int getMoney(WebBrowser b) 
        {
            HtmlElement Person = getPerson(b);
            if (Person == null) return -1;
            //HtmlElement wallet = WebParserEx.getTagByClassName(Person, "ul", "wallet");
            string tugriki = WebParserEx.getTagByClassName(Person, "li", "tugriki-block").GetAttribute("title");
            if(tugriki.Split(' ').Count() > 1) tugriki = tugriki.Split(' ')[1]; else tugriki = tugriki.Replace(",","");
            try
            {
                return int.Parse(tugriki);
            }
            catch (Exception)
            {
                Console.WriteLine("SkillsDetector::getMoney() - cannot parse tugriki=" + tugriki);
                return -1;
            }
        }

        public static int getHeath(WebBrowser b) 
        {
            HtmlElement Person = getPerson(b);
            if (Person == null) return -1;

            HtmlElement life = WebParserEx.getTagByClassName(Person, "div", "life");

            string strHealthCurrent = life.GetElementsByTagName("span")["currenthp"].InnerText;
            string strHealthMax = life.GetElementsByTagName("span")["maxhp"].InnerText;

            int healthCurrent = int.Parse(strHealthCurrent);
            int healthMax = int.Parse(strHealthMax);

            return (int)(healthCurrent * 100 / healthMax);
        }

        public static bool GetUserNameAndLevel(WebBrowser b, out string name, out int level) 
        {
            level = -1;
            name = "";
            HtmlElement person = getPerson(b);
            if (person == null) return false;
            name = WebParserEx.getTagByClassName(person, "a", "name").InnerText;
            string strLevel = name.Split(' ')[name.Split(' ').Count() - 1];
            name = name.Replace(" " + strLevel, "");
            strLevel = strLevel.Split('[')[1].Split(']')[0];
            return int.TryParse(strLevel, out level);
        }

        public static int getUserLevel(WebBrowser b) 
        {
            int level=-1;
            string name = "";
            if (GetUserNameAndLevel(b, out name, out level)) 
            {
                return level;
            }
            return -1;
        }

        public static string getUserName(WebBrowser b) 
        {
            string name = "";
            int level = -1;
            if (GetUserNameAndLevel(b, out name, out level)) 
            {
                return name;
            }
            return "";
        }

        public static HtmlElement getSkillCaptionByImageAddress(HtmlElement b, string imgSrc) 
        {
            foreach (HtmlElement thumb in b.GetElementsByTagName("div"))
            {
                if (thumb.GetAttribute("className") != "overtip") continue;
                HtmlElementCollection imgs = thumb.GetElementsByTagName("img");
                if (imgs.Count < 1) continue;
                if (imgs[0].GetAttribute("src") != imgSrc) continue;
                HtmlElement res = WebParserEx.getTagByClassName(thumb, "div", "object");
                return res;
            }
            return null;
        }

        public static bool getWaitTimeSpanAndAttributes(WebBrowser b, out TimeSpan ts, out string address)
        {
            ts = TimeSpan.Zero;
            address = "";
            HtmlElement links_clear = WebParserEx.getTagByClassName(b.Document.Body, "div", "links clear");
            if (links_clear == null) return false;
            HtmlElement timeout = links_clear.GetElementsByTagName("a")["timeout"];
            if (timeout == null) return true;
            string strTimeout = timeout.GetAttribute("timer");
            int iTimeout = 0;
            if (int.TryParse(strTimeout, out iTimeout)) return false;
            ts = TimeSpan.FromSeconds((double)iTimeout);
            address = timeout.GetAttribute("href");
            return true;
        }

        public static TimeSpan getWaitTimeSpan(WebBrowser browser)
        {
            string address;
            TimeSpan ts;
            if(!getWaitTimeSpanAndAttributes(browser,out ts,out address)) return TimeSpan.Zero;
            return ts;
        }
    }
}
